package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysCommunityBo;
import org.dromara.system.domain.vo.SysCommunityVo;
import org.dromara.system.service.ISysCommunityService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【小区台账维护】
 *
 * @author Lion Li
 * @date 2024-05-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/community")
public class SysCommunityController extends BaseController {

    private final ISysCommunityService sysCommunityService;

    /**
     * 查询小区列表
     */
    @SaCheckPermission("system:community:list")
    @GetMapping("/list")
    public TableDataInfo<SysCommunityVo> list(SysCommunityBo bo, PageQuery pageQuery) {
        return sysCommunityService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小区列表
     */
    @SaCheckPermission("system:community:export")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysCommunityBo bo, HttpServletResponse response) {
        List<SysCommunityVo> list = sysCommunityService.queryList(bo);
        ExcelUtil.exportExcel(list, "小区", SysCommunityVo.class, response);
    }

    /**
     * 获取小区详细信息
     *
     * @param communityId 主键
     */
    @SaCheckPermission("system:community:query")
    @GetMapping("/{communityId}")
    public R<SysCommunityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long communityId) {
        return R.ok(sysCommunityService.queryById(communityId));
    }

    /**
     * 新增小区
     */
    @SaCheckPermission("system:community:add")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysCommunityBo bo) {
        return toAjax(sysCommunityService.insertByBo(bo));
    }

    /**
     * 修改小区
     */
    @SaCheckPermission("system:community:edit")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysCommunityBo bo) {
        return toAjax(sysCommunityService.updateByBo(bo));
    }

    /**
     * 删除小区
     *
     * @param communityIds 主键串
     */
    @SaCheckPermission("system:community:remove")
    @Log(title = "小区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{communityIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] communityIds) {
        return toAjax(sysCommunityService.deleteWithValidByIds(List.of(communityIds), true));
    }
}
